######################################################
#Comparison figure for PA 2018 General (Main Model vs Dem Candidate Only Model).
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Packages.
library(ggplot2)
library(cowplot)

#setwd
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#Data.
PA<- read.csv("PA2018GeneralEffectsOnlyDemWomenRevised.csv", header=TRUE)
PA$Model<-c("Female Democratic\nCandidates")

PAgeneral<- read.csv("PA2018GeneralCompetitivenessControl.csv", header=TRUE)

PAgeneral$Registrant.Subgroup<-factor(c(1,2,3,4,5,6,7,8,9,10,11), levels=c(1,2,3,4,5,6,7,8,9,10,11),
                                      labels=c("Overall", "Women", "Men", "Democrats","Other\nParty", "Republicans","Gen. Z", "Millennials", "Gen. X", "Baby\nBoomers", "Silent Gen."), ordered = T)
PAgeneral$Model<-c("Main Model")

PAsum<-rbind(PA,PAgeneral)
PAsum$Registrant.Subgroup<-factor(c(1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11), levels=c(1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11),
                                  labels=c("Overall", "Women", "Men", "Democrats","Other\nParty", "Republicans","Gen. Z", "Millennials", "Gen. X", "Baby\nBoomers", "Silent Gen.",
                                           "Overall", "Women", "Men", "Democrats","Other\nParty", "Republicans","Gen. Z", "Millennials", "Gen. X", "Baby\nBoomers", "Silent Gen."), ordered = T)
#Figure.
pdf("PA2018ModelComparisonGeneralElectionsRevised.pdf", width=9, height=5)
MP<-ggplot(d=PAsum, aes(Registrant.Subgroup, Effect*100, shape=Model, color=Model))+
  geom_point(position = "dodge",
             stat = "identity")+
  geom_errorbar(aes(ymin=`UpperCI`*100, ymax=`LowerCI`*100),  width=0.5)+
  geom_hline(yintercept=0, linetype="dashed", color="gray28")+
  labs(y="Percentage Point Difference in Predicted Probability of Voting", x="Registrant Subgroup")+
  theme_bw()+theme(axis.text=element_text(size=8.5), axis.title=element_text(size=10))
ggdraw(add_sub(MP, x=0,hjust=0,
               "Notes: Estimates calculated using separate models. Error bars are 95% confidence intervals calculated using simulation \nholding covariates at their observed values. Estimates from the main model computed using results from table C.1. \nEstimates for female Democratic Candidates uses results from table C.2 (excludes district 5).", #add note.
               fontface = "italic", colour = "black", size=9)) #x=0.2 shifts it so its left justified.
dev.off()
